<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;
use app\common\controller\order\Orderget;
use app\common\model\Sum;

class Common extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $controller = request()->controller();
        $action = request()->action();
        $URL = 'admin/'.$controller.'/'.$action; //请求路径
        $URL = strtolower($URL);
        $default = [//默认显示页面
            'admin/count/default',
            'admin/count/index',
        ];
        if (!in_array($URL, $default)) {
            $menu_ID = Db::table('xb_sys_menu')->where(['Url' => $URL])->value('ID'); //请求路径对应的菜单ID
            if ($menu_ID) {
                $RoleID = Db::table('xb_sys_administrator')->where('ID', session('id'))->value('RoleID'); //角色ID
//				通过$RoleID去获取所拥有的菜单ID
                $menu_DATA = Db::table('xb_sys_role')->where(['ID' => $RoleID])->value('MenuID'); //请求路径对应的菜单ID
//				监测当前菜单ID 是否在菜单数组中
                $menu_DATA = explode(',', $menu_DATA);
                $re = in_array($menu_ID, $menu_DATA);
                if (!$re) {
                    show_error($code = '404', $message = '你要找的帅哥去泡妞了,明天早点来', $data = []);

                    return $this->redirect('http://www.yy.com/22490906/22490906?tempId=16777217');
                }
            }
        }

        //验证是否免登陆
        $action = strtolower(Request::instance()->action());
        $checkLoginData = config('queue.checkLogin');
        if (!in_array($action, $checkLoginData)) {
            //验证是否是登录状态 没登录就跳转到登录页面
            if (!session('id')) {
                return $this->redirect('admin.php/admin/login/login');
            }
        }

        //输出系统信息
        $systemInfo = (new System())->getSystemInfo();
        //获取充值总额
        $systemInfo['sumCharge'] = db('sys_charge')->where(['Status' => 1])->sum('Money');
        //获取消费总额
        $systemInfo['cost'] = db('docking_log')->sum('Price');
        //计算剩余金额
        $systemInfo['surplus'] = $systemInfo['sumCharge'] - $systemInfo['cost'];
        $this->assign('systemInfo', $systemInfo);

        if ($systemInfo['surplus'] < $systemInfo['BalanceInterval']) {
//            $isAlert = cache('true',3600);
            $isAlert = 1;
        } else {
            $isAlert = 0;
        }
        $this->assign('isAlert', $isAlert);
        $this->assign('systemInfo', $systemInfo);


//        需求暂停了
//        $sumModelObj = new Sum();
//        //检测 推广渠道 0分率 是否超过15%
//        $sumModelObj->checkExtensionBranch();
//        //检测 推广渠道 申请率 是否超过70%
//        $sumModelObj->checkExtensionApply();
//        //检测 渠道统计 0分率 是否超过15%
//        $sumModelObj->checkChannelBranch();
//        //检测 渠道统计 申请率 是否超过70%;
//        $sumModelObj->checkChannelApply();

    }

    //记录操作记录  $o 操作描叙
    public function logs($o)
    {
        $controller = request()->controller();
        $action = request()->action();
        $id = Session::get('id');
        $admin = Db::table('xb_sys_administrator')->where('ID', $id)->find();
        $data = array(
            'DateTime' => date('Y-m-d H:i:s'),
            'IP' => $admin['LoginIP'],
            'IPCity' => $admin['IpCity'],
            'Des' => $o,
            'UserID' => $id,
            'TrueName' => $admin['TrueName'],
            'UserName' => $admin['UserName'],
            'ControllerName' => $controller,
            'ActionName' => $action,
            'URL' => 'admin/'.$controller.'/'.$action,
        );

        //		p($data);die;
        try {
            $result = Db::table('xb_sys_log')->insert($data);
        } catch (Exception $e) {
            echo 'Logo Error_july: '.$e->getMessage();
        }
    }

    //	订单详情
    public function detail()
    {
        $this->get_xingqing();

        return $this->fetch('orderhuankuan/detail');
    }

    //	详情里面的订单里面的 展期记录页面
    public function zhanqi_list()
    {
        $data = input();
        $this->assign('data', $data);

        return $this->fetch('orderhuankuan/zhanqi_list');
    }

    public function huankuan_list()
    {
        $data = input();
        $this->assign('data', $data);

        return $this->fetch('orderhuankuan/huankuan_list');
    }

    //	获取渠道信息
    public function qudao()
    {
        $qudao = Db::table('xb_tg_admin')->select();

        return $qudao;
    }

    //订单详情
    public function get_xingqing()
    {
        $ID = input(); //订单ID
        //		订单数据
        $applylist_data = Db::table('xb_loans_applylist a')
         ->join('xb_mem_info b', 'a.UserID=b.ID', 'LEFT')
         ->where('a.ID', $ID['ID'])
         ->field('a.*,b.ID as bID,b.TrueName,b.Mobile')
         ->find();
        //		 p($applylist_data);die;
        if ($applylist_data['cuishourenID']) {
            $applylist_data['cuishourenID'] = Db::table('xb_sys_administrator')->where(['ID' => $applylist_data['cuishourenID']])->value('UserName'); //用户ID
        }
        //		 支付方式（放款方式） 1微信 2支付宝 3银行卡(后台手动放款)4银行卡(自动放款)
        if ($applylist_data['pay'] == '1') {
            $applylist_data['pay'] = '微信';
        } elseif ($applylist_data['pay'] == '2') {
            $applylist_data['pay'] = '支付宝';
        } elseif ($applylist_data['pay'] == '3') {
            $applylist_data['pay'] = '银行卡（后台手动放款）';
        } elseif ($applylist_data['pay'] == '4') {
            $applylist_data['pay'] = '银行卡（自动放款）';
        }
        $Orderget = new Orderget();
        if ($applylist_data['OpenTime']) {
            $get_days_fakuan = $Orderget->get_days_fakuan(strtotime(date('Y-m-d'), time()), strtotime($applylist_data['YyFkTime']), $applylist_data);
            $applylist_data['yuqililv'] = $applylist_data['yuqililv'].'%'; //逾期利率
        $applylist_data['datas'] = $get_days_fakuan['datas']; //逾期天数
        $applylist_data['fakuan'] = $get_days_fakuan['fakuan']; //罚款
        } else {
            $applylist_data['yuqililv'] = '0%'; //逾期利率
        $applylist_data['datas'] = 0; //逾期天数
        $applylist_data['fakuan'] = 0; //罚款
        }

        if ($applylist_data['payCome'] == '1') {
            $applylist_data['payCome'] = '线下微信'; //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
        } elseif ($applylist_data['payCome'] == '2') {
            $applylist_data['payCome'] = '线下支付宝'; //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
        } elseif ($applylist_data['payCome'] == '3') {
            $applylist_data['payCome'] = '线下银行'; //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
        } elseif ($applylist_data['payCome'] == '4') {
            $applylist_data['payCome'] = '线上微信'; //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
        } elseif ($applylist_data['payCome'] == '5') {
            $applylist_data['payCome'] = '线上支付宝'; //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
        } elseif ($applylist_data['payCome'] == '6') {
            $applylist_data['payCome'] = '线上银联'; //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
        } elseif ($applylist_data['payCome'] == '7') {
            $applylist_data['payCome'] = '豁免'; //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
        } else {
            $applylist_data['payCome'] = '未还款';
        }

        $applylist_data['fakuan'] = $applylist_data['fakuan']; //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
        $applylist_data['jianmian_money'] = $Orderget->get_jianmian_money($applylist_data['OrderSn']); // 应还减免
        $applylist_data['bufen_money'] = $Orderget->get_bufen_money($applylist_data['OrderSn']); //部分已还
        $applylist_data['xjnums_money'] = $applylist_data['zhanqifei'] * $applylist_data['xjnums']; //展期金额
        $applylist_data['huankuan_mpney'] = Db::table('xb_loans_hklist')->where(['ApplyID' => $applylist_data['ID']])->sum('TotalMoney'); //用户ID
        $Orderget = new Orderget();
        $applylist_data['dingdan_yinghuan_money'] = $Orderget->get_dingdan_yinghuan_money($applylist_data); //订单应还
        $applylist_data['muqian_yinghuan_money'] = $Orderget->get_muqian_yinghuan_money($applylist_data); // 目前应还
        $applylist_data['yuqimoney'] = $Orderget->get_yinghuan_yuqi($applylist_data); //获取剩余应还逾期金额

//		p($applylist_data);die;
        $this->assign('applylist_data', $applylist_data);

        //		用户数据
        $ID['ID'] = Db::table('xb_loans_applylist')->where('ID', $ID['ID'])->value('UserID');
        //用户ID
        $mem_info = Db::table('xb_mem_info')->where('ID', $ID['ID'])->find();
        //		会员状态 1待审核 2审核通过 3禁用 4黑名单
        if ($mem_info['Status'] == '1') {
            $mem_info['Status'] = '<span style="color:green;">待审核</span>';
        } elseif ($mem_info['Status'] == '2') {
            $mem_info['Status'] = '<span style="color:green;">审核通过</span>';
        } elseif ($mem_info['Status'] == '3') {
            $mem_info['Status'] = '<span style="color:red;">禁用</span>';
        } elseif ($mem_info['Status'] == '4') {
            $mem_info['Status'] = '<span style="color:red;">黑名单</span>';
        }
        $this->assign('mem_info', $mem_info);
        //基本信息

        //      身份证认证
        $renzen_cards = Db::table('xb_renzen_cards')->where('UserID', $ID['ID'])->find();
        $Yddatas = json_decode($renzen_cards['Yddatas'], 1);
        $Yddatas = $Yddatas['data'];
        //		p($Yddatas);die;
        $cardimgArr['CardFace'] = $renzen_cards['CardFace'];
        $cardimgArr['CardSide'] = $renzen_cards['CardSide'];
        $cardimgArr['Cardschi'] = $renzen_cards['Cardschi'];
        if ($renzen_cards['Status'] == '1') {
            $renzen_cards['Status'] = '<span style="color:green;">已认证</span>';
        } elseif ($renzen_cards['Status'] == '2') {
            $renzen_cards['Status'] = '<span style="color:red;">未认证</span>';
        }
        $this->assign('renzen_cards', $renzen_cards);
        //身份证信息
        $this->assign('Yddatas', $Yddatas);
        //身份证信息
        $this->assign('cardimgArr', $cardimgArr);
        //身份证照片

        //      手机认证
        $renzen_mobile = Db::table('xb_renzen_mobile')->where('UserID', $ID['ID'])->find();
        if ($renzen_mobile['Status'] == '1') {
            $renzen_mobile['Status'] = '<span style="color:green;">已认证</span>';
        } elseif ($renzen_mobile['Status'] == '2') {
            $renzen_mobile['Status'] = '<span style="color:red;">未认证</span>';
        }
        $dataaa = Db::table('xb_renzen_social')->where('UserID', $ID['ID'])->find();

        $renzen_mobile['url'] = 'https://tenant.51datakey.com/carrier/mxreport_data?data='.$renzen_mobile['mistr'].'&contact='.$dataaa['person_one_mobile'].':'.$dataaa['person_one'].':'.$dataaa['r1'].','.$dataaa['person_two_mobile'].':'.$dataaa['person_two'].':'.$dataaa['r2'];
        $renzen_mobile['urlyunyingshang'] = 'https://tenant.51datakey.com/carrier/report_data?data='.$renzen_mobile['mistr'].'&contact='.$dataaa['person_one_mobile'].':'.$dataaa['person_one'].':'.$dataaa['r1'].','.$dataaa['person_two_mobile'].':'.$dataaa['person_two'].':'.$dataaa['r2'];

        $this->assign('renzen_mobile', $renzen_mobile);
        //      手机认证数据

        //      社交认证
        $renzen_social = Db::table('xb_renzen_social')->where('UserID', $ID['ID'])->find();
        if ($renzen_social['Status'] == '1') {
            $renzen_social['Status'] = '<span style="color:green;">已认证</span>';
        } elseif ($renzen_social['Status'] == '2') {
            $renzen_social['Status'] = '<span style="color:red;">未认证</span>';
        }
        if ($renzen_social['marry'] == '0') {
            $renzen_social['marry'] = '<span style="color:red;">未婚</span>';
        } elseif ($renzen_social['marry'] == '1') {
            $renzen_social['marry'] = '<span style="color:red;">已婚</span>';
        }
        $this->assign('renzen_social', $renzen_social);
        //      社交认证数据

        $renzen_bank = Db::table('xb_renzen_bank')->where('UserID', $ID['ID'])->find();
        if ($renzen_bank['Status'] == '1') {
            $renzen_bank['Status'] = '<span style="color:green;">已认证</span>';
        } elseif ($renzen_bank['Status'] == '2') {
            $renzen_bank['Status'] = '<span style="color:red;">未认证</span>';
        }
        $this->assign('renzen_bank', $renzen_bank);
        //      银行卡认证数据

        //      订单状态  1放款中 2已放款 3已完成 4已取消 5已拒绝 6等待银行打款7待人工审核
        //      订单数据
        $loans_applylist = Db::table('xb_loans_applylist')->where('UserID', $ID['ID'])->select();
        foreach ($loans_applylist as $k => $v) {
            if ($v['LoanStatus'] == '1') {
                $v['LoanStatus'] = '<span style="color:green;">放款中</span>';
            } elseif ($v['LoanStatus'] == '2') {
                $v['LoanStatus'] = '<span style="color:red;">已放款</span>';
            } elseif ($v['LoanStatus'] == '3') {
                $v['LoanStatus'] = '<span style="color:red;">已完成</span>';
            } elseif ($v['LoanStatus'] == '4') {
                $v['LoanStatus'] = '<span style="color:red;">已取消</span>';
            } elseif ($v['LoanStatus'] == '5') {
                $v['LoanStatus'] = '<span style="color:red;">已拒绝</span>';
            } elseif ($v['LoanStatus'] == '6') {
                $v['LoanStatus'] = '<span style="color:red;">等待银行打款</span>';
            } elseif ($v['LoanStatus'] == '7') {
                $v['LoanStatus'] = '<span style="color:red;">待人工审核</span>';
            }
            $loans_applylist[$k]['LoanStatus'] = $v['LoanStatus'];
        }
        //				p($loans_applylist);die;
        $this->assign('loans_applylist', $loans_applylist);
        //订单数据
    }

    //用户详情
    public function get_xingqing_yonghu()
    {
        $ID = input(); //用户ID
        //		用户数据
        //用户ID
        $mem_info = Db::table('xb_mem_info')->where(['ID' => $ID['ID']])->find();
        //		会员状态 1待审核 2审核通过 3禁用 4黑名单
        if ($mem_info['Status'] == '1') {
            $mem_info['Status'] = '<span style="color:green;">待审核</span>';
        } elseif ($mem_info['Status'] == '2') {
            $mem_info['Status'] = '<span style="color:green;">审核通过</span>';
        } elseif ($mem_info['Status'] == '3') {
            $mem_info['Status'] = '<span style="color:red;">禁用</span>';
        } elseif ($mem_info['Status'] == '4') {
            $mem_info['Status'] = '<span style="color:red;">黑名单</span>';
        }

        $this->assign('mem_info', $mem_info);
        //基本信息

        //      身份证认证
        $renzen_cards = Db::table('xb_renzen_cards')->where('UserID', $ID['ID'])->find();
        if ($renzen_cards) {
            $Yddatas = json_decode($renzen_cards['Yddatas'], 1);
            $Yddatas = $Yddatas['data'];
            //		p($Yddatas);die;
            $cardimgArr['CardFace'] = $renzen_cards['CardFace'];
            $cardimgArr['CardSide'] = $renzen_cards['CardSide'];
            $cardimgArr['Cardschi'] = $renzen_cards['Cardschi'];
            if ($renzen_cards['Status'] == '1') {
                $renzen_cards['Status'] = '<span style="color:green;">已认证</span>';
            } elseif ($renzen_cards['Status'] == '2') {
                $renzen_cards['Status'] = '<span style="color:red;">未认证</span>';
            }
            $this->assign('renzen_cards', $renzen_cards);
            //身份证信息
            $this->assign('Yddatas', $Yddatas);
            //身份证信息
            $this->assign('cardimgArr', $cardimgArr);

            //		p($renzen_cards);
//		p($Yddatas);
//		p($cardimgArr);die;
        }
        //身份证照片

        //      手机认证
        $renzen_mobile = Db::table('xb_renzen_mobile')->where('UserID', $ID['ID'])->find();
        if ($renzen_mobile) {
            if ($renzen_mobile['Status'] == '1') {
                $renzen_mobile['Status'] = '<span style="color:green;">已认证</span>';
            } elseif ($renzen_mobile['Status'] == '2') {
                $renzen_mobile['Status'] = '<span style="color:red;">未认证</span>';
            }
            $dataaa = Db::table('xb_renzen_social')->where('UserID', $ID['ID'])->find();

            $renzen_mobile['url'] = 'https://tenant.51datakey.com/carrier/mxreport_data?data='.$renzen_mobile['mistr'].'&contact='.$dataaa['person_one_mobile'].':'.$dataaa['person_one'].':'.$dataaa['r1'].','.$dataaa['person_two_mobile'].':'.$dataaa['person_two'].':'.$dataaa['r2'];
            $renzen_mobile['urlyunyingshang'] = 'https://tenant.51datakey.com/carrier/report_data?data='.$renzen_mobile['mistr'].'&contact='.$dataaa['person_one_mobile'].':'.$dataaa['person_one'].':'.$dataaa['r1'].','.$dataaa['person_two_mobile'].':'.$dataaa['person_two'].':'.$dataaa['r2'];

            $this->assign('renzen_mobile', $renzen_mobile);
        }
        //      手机认证数据

        //      社交认证
        $renzen_social = Db::table('xb_renzen_social')->where('UserID', $ID['ID'])->find();
        if ($renzen_social['Status'] == '1') {
            $renzen_social['Status'] = '<span style="color:green;">已认证</span>';
        } elseif ($renzen_social['Status'] == '2') {
            $renzen_social['Status'] = '<span style="color:red;">未认证</span>';
        }
        if ($renzen_social['marry'] == '0') {
            $renzen_social['marry'] = '<span style="color:red;">未婚</span>';
        } elseif ($renzen_social['marry'] == '1') {
            $renzen_social['marry'] = '<span style="color:red;">已婚</span>';
        }

        //		p($renzen_social);die;
        $this->assign('renzen_social', $renzen_social);
        //      社交认证数据

        $renzen_bank = Db::table('xb_renzen_bank')->where('UserID', $ID['ID'])->find();
        if ($renzen_bank['Status'] == '1') {
            $renzen_bank['Status'] = '<span style="color:green;">已认证</span>';
        } elseif ($renzen_bank['Status'] == '2') {
            $renzen_bank['Status'] = '<span style="color:red;">未认证</span>';
        }
        $this->assign('renzen_bank', $renzen_bank);
        //      银行卡认证数据

        //      订单状态  1放款中 2已放款 3已完成 4已取消 5已拒绝 6等待银行打款7待人工审核
        //      订单数据
        $loans_applylist = Db::table('xb_loans_applylist')->where('UserID', $ID['ID'])->select();
        foreach ($loans_applylist as $k => $v) {
            if ($v['LoanStatus'] == '1') {
                $v['LoanStatus'] = '<span style="color:green;">放款中</span>';
            } elseif ($v['LoanStatus'] == '2') {
                $v['LoanStatus'] = '<span style="color:red;">已放款</span>';
            } elseif ($v['LoanStatus'] == '3') {
                $v['LoanStatus'] = '<span style="color:red;">已完成</span>';
            } elseif ($v['LoanStatus'] == '4') {
                $v['LoanStatus'] = '<span style="color:red;">已取消</span>';
            } elseif ($v['LoanStatus'] == '5') {
                $v['LoanStatus'] = '<span style="color:red;">已拒绝</span>';
            } elseif ($v['LoanStatus'] == '6') {
                $v['LoanStatus'] = '<span style="color:red;">等待银行打款</span>';
            } elseif ($v['LoanStatus'] == '7') {
                $v['LoanStatus'] = '<span style="color:red;">待人工审核</span>';
            }
            $loans_applylist[$k]['LoanStatus'] = $v['LoanStatus'];
        }
        //		p($loans_applylist);die;
        $this->assign('loans_applylist', $loans_applylist);
        //订单数据
    }
}
